package com.ai.trickycards.widgets

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.annotation.IntRange
import androidx.constraintlayout.widget.ConstraintLayout
import com.ai.trickycards.databinding.LayoutPlayerViewBinding
import com.ai.trickycards.entity.enumeration.PlayerEnum
import kotlin.math.roundToInt

class PlayerView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private val binding = LayoutPlayerViewBinding.inflate(LayoutInflater.from(context), this)
    private val density = context.resources.displayMetrics.density

    val pokerCardCompose = binding.pokerCardCompose

    init {
        clipChildren = false
    }

    fun resize(@IntRange(from = 3, to = 5) playerCount: Int) {
        println("playerCount=$playerCount")
        var headSize = 80 * density
        var nickSize = 12 * density
        var pokerWidth = 24 * density
        var pokerHeight = 36 * density
        if (playerCount >= 4) {
            headSize = 68 * density
            nickSize = 11 * density
            pokerWidth = 20 * density
            pokerHeight = 30 * density
        }
        val paramsHead = binding.ivHead.layoutParams
        paramsHead.width = headSize.roundToInt()
        paramsHead.height = headSize.roundToInt()
        binding.ivHead.layoutParams = paramsHead

        binding.tvNick.setTextSize(TypedValue.COMPLEX_UNIT_PX, nickSize)

        binding.pokerCardCompose.pokerWidth = pokerWidth.roundToInt()
        binding.pokerCardCompose.pokerHeight = pokerHeight.roundToInt()
    }

    fun setProfile(player: PlayerEnum) {
        binding.ivHead.setImageResource(player.head)
        binding.tvNick.text = player.nick
    }

    fun setLives(lives: Int) {
        binding.tvLives.text = "$lives"
    }
}